﻿using System.ComponentModel;
using GeneratorLib;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;


namespace Generator.Generators
{
    public partial class GeneratorCodeBase : GeneratorBase
    {
        public CodeBuilder CodeBuilder = new CodeBuilder();
        protected string SaveFileName = string.Empty;
        public GeneratorCodeBase()
        {
            InitializeComponent();
            InitEditor();
        }

        private TextEditorControl ActiveEditor1
        {
            get
            {
                return this.txtCode;
            }
        }

        private void InitEditor()
        {
            if (ActiveEditor1 != null)
            {
                ActiveEditor1.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#");
            }
        }

        //public override void Generate()
        //{
        //    codeBuilder.Clear();
        //    base.Generate();
        //}

        //public override void Generate(ToolStripItem item)
        //{
        //    codeBuilder.Clear();
        //    base.Generate(item);
        //}

        #region the code that every class will append
        protected virtual void AppendUsing()
        {
            CodeBuilder.AppendLine("using System;");
            CodeBuilder.AppendLine("using System.Collections.Generic;");
            CodeBuilder.AppendLine("using System.Linq;");
            CodeBuilder.AppendLine("using System.Text;");
            CodeBuilder.AppendLine("using System.Data;");
            CodeBuilder.AppendLine("using System.Data.SqlClient;");
            CodeBuilder.AppendLine("using PacificSystem.DatabaseLibrary;");
            CodeBuilder.AppendLine("using PacificSystem.Utility;");
            CodeBuilder.AppendLine("using PacificSystem.ServerLibrary;");
            CodeBuilder.AppendLine("using PacificSystem.ConstantLibrary;");
            CodeBuilder.AppendLine("using MySql.Data.MySqlClient;");

        }

        protected virtual void AppendNameSpaceStart()
        {
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("namespace PacificSystem.Core.ServerControls");
            CodeBuilder.AppendLine("{");

        }

        protected virtual void AppendNameSpaceEnd()
        {
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("}");

        }

        protected virtual void AppandClassStart(string classAdditional, string baseClassName, string functionDesc)
        {
            CodeBuilder.AppendLine("    /// <summary>");
            CodeBuilder.AppendLine("    /// {0}", functionDesc); // 
            CodeBuilder.AppendLine("    /// </summary>");
            if (baseClassName.Trim().Length > 0)
            {
                CodeBuilder.AppendLine("    public class {0}{1}:{2} ", this.Table.EntityName, classAdditional, baseClassName);
            }
            else
            {
                CodeBuilder.AppendLine("    public class {0}{1} ", this.Table.EntityName, classAdditional);
            }
                
            SaveFileName = string.Format("{0}{1}", this.Table.EntityName, classAdditional);
            CodeBuilder.AppendLine("    {");

        }

        protected virtual void AppandClassEnd()
        {
            CodeBuilder.AppendLine("    }");
        }

        protected virtual void AppendMethodStart()
        {
            CodeBuilder.AppendLine("        /// <summary>");
            CodeBuilder.AppendLine("        /// </summary>");
            CodeBuilder.AppendLine("        public void {0}()", this.Table.EntityName);
            CodeBuilder.AppendLine("        {");
        }

        protected virtual void AppendMethodEnd()
        {
            CodeBuilder.AppendLine("        }");
        }
        #endregion


        public override void Save()
        {
            base.Save();
            PrepareSave();
            saveFileDialog1.ShowDialog();
        }

        protected virtual void PrepareSave()
        {
            saveFileDialog1.FileName = string.Format("{0}.cs", SaveFileName);
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string fileName = saveFileDialog1.FileName;
            string fileName2 = fileName + ".tmp";
            
            try
            {
                if (System.IO.File.Exists(fileName2))
                    System.IO.File.Delete(fileName2);

                txtCode.SaveFile(fileName2);
                System.IO.File.Copy(fileName2, fileName, true);
            }
            catch
            { }
        }

    }
}
